<template>
  <div>
    <!-- 列表 -->
    <ul class="TemplateStyle">
      <!-- 入库日期 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/date.png" />
          <span class="li_txt">入库日期</span>
        </div>
        <div class="label">
          <input type="text" :value="userDate" :disabled="true" />
        </div>
      </li>
      <!-- 爱心码 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/loveCode.png" />
          <span class="li_txt">爱心码</span>
        </div>
        <div class="label">
          <input type="number" @blur="Mouse_out" v-model="donateId" />
        </div>
      </li>
      <!-- 入库单号 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/symbol.png" />
          <span class="li_txt">入库单号</span>
        </div>
        <div class="label">
          <input type="number" :value="inId" :disabled="true" />
        </div>
      </li>

      <!-- 捐赠方 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/donation.png" />
          <span class="li_txt">捐赠方(公司名称)</span>
        </div>
        <div class="label">
          <input type="text" :value="donator" :disabled="true" />
        </div>
      </li>
      <!-- 承运方 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/transport.png" />
          <span class="li_txt">承运方</span>
        </div>
        <div class="label">
          <input type="text" :value="shipper" :disabled="true" />
        </div>
      </li>
      <!-- 收货人 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/consignee.png" />
          <span class="li_txt">收货人</span>
        </div>
        <div class="label">
          <input type="text" v-model="receiver" />
        </div>
      </li>
      <!-- 快递单号 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/symbol.png" />
          <span class="li_txt">快递单号</span>
        </div>
        <div class="label">
          <input type="number" v-model="shipId" />
        </div>
      </li>

      <!-- 附件 -->
      <li>
        <div class="label">
          <img class="li_icon" src="../../assets/image/Enclosure.png" />
          <span class="li_txt">附件</span>
        </div>
        <van-uploader accept="image/png, image/jpeg, image/jpg" :after-read="afterRead">
          <van-button
            style="border-radius:.8rem;height:2.1rem;line-height:2.1rem"
            icon="photo"
            type="primary"
          >上传领用原始凭证</van-button>
        </van-uploader>
      </li>
      <!-- 确认按钮 -->
      <button class="btn" @click="btnInStorageA">确定</button>
    </ul>
    <!-- loading -->
    <div style="padding-bottom: 4rem; position:top:50%">
      <van-loading
        v-show="loading"
        style="position: absolute;top: 50%;left: 46%;"
        type="spinner"
        color="#1989fa"
      ></van-loading>
    </div>
  </div>
</template>
<script>
import moment from "moment";

export default {
  name: "InStorageA",
  data() {
    return {
      userDate: "", // 展示给用户
      inTime: "",
      donateId: "",
      donator: "",
      inId: "",
      shipper: "",
      receiver: "",
      shipId: "",
      img: "",
      loading: false // loading
    };
  },
  created() {
    // 自动生成日期
    let nowDate = new Date();
    this.inTime = moment(nowDate).format("YYYY-MM-DD HH:mm:ss");
    this.userDate = moment(nowDate).format("YYYY-MM-DD");
    // 随机生成入库单号
    let random = "";
    for (let a = 0; a < 20; a++) {
      random += Math.floor(Math.random() * 10);
    }
    this.inId = random;
  },
  methods: {
    // 根据爱心码自动填写内容
    Mouse_out() {
      if (!this.donateId) {
        this.donator = "";
        this.shipper = "";
      } else if (this.donateId != null) {
        let loveCode = {
          userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
          token: JSON.parse(localStorage.getItem("login")).res.token,
          donateId: this.donateId
        };
        //通过爱心码调用物资捐赠者信息;
        this.$api
          .getDonateProductById(loveCode)
          .then(res => {
            this.donator = res[0].userName;
            this.shipper = res[0].shipper;
          })
          .catch(err => {
            this.$dialog.alert({
              message: "未查询到该爱心码"
            });
            this.donator = "";
            this.shipper = "";
          });
      }
    },

    // 上传图片
    afterRead(file) {
      this.img = file.content;
    },
    btnInStorageA() {
      this.loading = true; // loading显示
      let params = {
        token: JSON.parse(localStorage.getItem("login")).res.token,
        userName: JSON.parse(localStorage.getItem("login")).res.user.userName,
        inTime: this.inTime,
        donateId: this.donateId,
        donator: this.donator,
        inId: this.inId,
        shipper: this.shipper,
        receiver: this.receiver,
        shipId: this.shipId,
        attachment: this.img
      };
      this.$api
        .requestProductIn(params)
        .then(res => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: res
          });
        })
        .catch(err => {
          this.loading = false; // loading隐藏
          this.$dialog.alert({
            message: err
          });
        });
    }
  }
};
</script>
<style>
@import "../../assets/css/TemplateStyle.css";
</style>